package mi.payment.settings;

import android.text.TextUtils;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * @author sunmi on 2017/2/16.
 */
public class SalesSlipPrintSettingsOperation {

    private static SalesSlipPrintSettingsOperation instance;
    private SalesSlipPrintSettings instanceSettings;

    private SalesSlipPrintSettingsOperation() {
        instanceSettings = getSalesSlipPrintSettings();
        if (instanceSettings == null) {
            instanceSettings = new SalesSlipPrintSettings();
            saveSalesSlipPrintSettingsOperation(instanceSettings);
        }
    }

    public static SalesSlipPrintSettingsOperation getInstance() {
        if (instance == null) {
            instance = new SalesSlipPrintSettingsOperation();
        }
        return instance;
    }

    public void init(int rise, String hotline,
                     String riseString,
                     String printReceiptSingleNum,
                     boolean acquiringBankPrintChinese,
                     boolean issuingBankPrintChinese,
                     boolean salesSlipPrintChinese) {
        SalesSlipPrintSettings bean = getSalesSlipPrintSettings();
        bean.setRise(rise);
        bean.setHotLine(hotline);
        bean.setRiseString(riseString);
        bean.setPrintReceiptSingleNum(printReceiptSingleNum);
        bean.setAcquiringBankPrintChinese(acquiringBankPrintChinese);
        bean.setIssuingBankPrintChinese(issuingBankPrintChinese);
        bean.setSalesSlipPrintEnglish(salesSlipPrintChinese);
        saveSalesSlipPrintSettingsOperation(bean);
    }

    public void saveSalesSlipPrintSettingsOperation(SalesSlipPrintSettings mSalesSlipPrintSettings) {
        this.instanceSettings = mSalesSlipPrintSettings;
        SharedPreferencesUtil.saveObj(mSalesSlipPrintSettings, SalesSlipPrintSettings.SALES_SLIP_PRINT_SETTINGS_KEY);
    }

    public SalesSlipPrintSettings getSalesSlipPrintSettings() {
        if (instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(SalesSlipPrintSettings.SALES_SLIP_PRINT_SETTINGS_KEY);
            if (obj instanceof SalesSlipPrintSettings) {
                instanceSettings = (SalesSlipPrintSettings) obj;
            } else {
                instanceSettings = new SalesSlipPrintSettings();
                saveSalesSlipPrintSettingsOperation(instanceSettings);
            }
        }
        return instanceSettings;
    }

    /**
     * 设置打印凭条单联数
     *
     * @param printReceiptSingleNum 打印凭条单联数
     */
    public void savePrintReceiptSingleNum(String printReceiptSingleNum) {
        if (TextUtils.isEmpty(printReceiptSingleNum)) {
            return;
        }
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setPrintReceiptSingleNum(printReceiptSingleNum);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    /**
     * 收单行打印中文
     *
     * @param acquiringBankPrintChinese 是否打印中文
     */
    public void saveAcquiringBankPrintChinese(boolean acquiringBankPrintChinese) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setAcquiringBankPrintChinese(acquiringBankPrintChinese);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    /**
     * 发卡行打印中文
     *
     * @param issuingBankPrintChinese 是否打印中文
     */
    public void saveIssuingBankPrintChinese(boolean issuingBankPrintChinese) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setIssuingBankPrintChinese(issuingBankPrintChinese);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    /**
     * 签购单是否打印英文
     *
     * @param salesSlipPrintEnglish 是否打印英文
     */
    public void saveSalesSlipPrintEnglish(boolean salesSlipPrintEnglish) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setSalesSlipPrintEnglish(salesSlipPrintEnglish);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    /**
     * 是否打印副标题
     *
     * @param isPrintSubtitle
     */
    public void savePrintSubtitle(boolean isPrintSubtitle) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setPrintSubtitle(isPrintSubtitle);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveRise(int rise) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setRise(rise);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveIsPrintChannelInfo(boolean print) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setPrintChannelInfo(print);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveHotLine(String hot) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setHotLine(hot);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveRiseString(String riseString) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setRiseString(riseString);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveSelectSalesSlipFont(int fontType) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setSelectSalesSlipFont(fontType);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveScanCodeSalesSlip(int type) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setScanCodeSalesSlip(type);
        saveSalesSlipPrintSettingsOperation(sales);
    }

    public void saveScanCodePrint(boolean isPrint) {
        SalesSlipPrintSettings sales = getSalesSlipPrintSettings();
        sales.setScanCodePrint(isPrint);
        saveSalesSlipPrintSettingsOperation(sales);
    }
}
